package in.muton.edushare.auth.roles;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.edushare.common.vo.CourseDeptLink;
import com.edushare.common.vo.CourseInstances;
import com.edushare.common.vo.Enrolments;
import com.edushare.common.vo.UniversityCourseLinks;
import com.edushare.common.vo.Users;

public class CourseRoleCheck {

	private static final Log log = LogFactory.getLog(CourseRoleCheck.class);

	public CourseRoleCheck(CourseInstances course, Users user) {
		this.course = course;
		this.user = user;
	}

	public boolean isAdmin() {
		return isAdmin(course, user);
	}

	public boolean isTeacher() {
		return isTeacher(course, user);
	}

	public boolean isStudent() {
		return isStudent(course, user);
	}
	
	public boolean isTA(){
		return isTA(course, user);
	}
	
	public boolean isMember(){
		return getRole() != 0;
	}
	
	public int getRole(){
		return getRole(course,user);
	}

	public static int getRole(CourseInstances course, Users user) {
		List<Enrolments> enrol = user.getEnrolments();
		for (Enrolments e : enrol) {
			if (e.getCourseInstance() == course
					&& e.getStatus() != Enrolments.INVITED)
				return e.getEnrolmentType();
		}
		return 0;
	}
	
	public static boolean isMember(CourseInstances course, Users user){
		return getRole(course, user) == 0;
	}

	public static boolean isTeacher(CourseInstances course, Users user) {
		return getRole(course, user) == Enrolments.TEACHER
				|| getRole(course, user) == Enrolments.TEACHING_ASSISTANT;
	}

	public static boolean isStudent(CourseInstances course, Users user) {
		return getRole(course, user) == Enrolments.STUDENT ||
			getRole(course, user) == Enrolments.OBSERVER;
	}
	
	public static boolean isTA(CourseInstances course, Users user) {
			return getRole(course, user) == Enrolments.TEACHING_ASSISTANT;
	}

	public static boolean isDeptMember(CourseInstances course, Users user) {
		List<CourseDeptLink> cdl = course.getCourse().getDeptLinks();
		for (CourseDeptLink c : cdl) {
			if (c.getCourseDeptAssnType() == CourseDeptLink.DEPT_COURSE_OWNER) {
				if (DeptRoleCheck.getRole(c.getDepartment(), user) != 0)
					return true;
			}
		}

		return false;
	}

	public static boolean isUnivMember(CourseInstances course, Users user) {
		List<UniversityCourseLinks> ucl = course.getCourse().getUniversities();
		for (UniversityCourseLinks u : ucl) {
			if (u.getUnivCourseAssnType() == UniversityCourseLinks.UNIV_COURSE_OWNER) {
				if (UnivRoleCheck.getRole(u.getUniversity(), user) != 0)
					return true;
			}
		}
		return false;
	}

	public static boolean isAdmin(CourseInstances course, Users user) {
		List<CourseDeptLink> cdl = course.getCourse().getDeptLinks();
		for (CourseDeptLink c : cdl) {
			if (c.getCourseDeptAssnType() == CourseDeptLink.DEPT_COURSE_OWNER) {
				log.debug("CourseRoleCheck:isAdmin:Department = "
						+ c.getDepartment().getDeptName());
				if (DeptRoleCheck.isPrimaryAdmin(c.getDepartment(), user))
					return true;
				;
			}
		}

		List<UniversityCourseLinks> ucl = course.getCourse().getUniversities();
		for (UniversityCourseLinks u : ucl) {
			if (u.getUnivCourseAssnType() == UniversityCourseLinks.UNIV_COURSE_OWNER) {
				log.debug("CourseRoleCheck:isAdmin:University = "
						+ u.getUniversity().getUniversityName());
				if (UnivRoleCheck.isAdmin(u.getUniversity(), user))
					return true;
			}
		}

		return false;
	}

	private CourseInstances course;
	private Users user;
}
